package xyz.weizhi.tanhua.dubbo.api;

import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import xyz.weizhi.tanhua.model.enums.CommentType;
import xyz.weizhi.tanhua.model.mongo.Comment;
import xyz.weizhi.tanhua.model.mongo.Movement;
import xyz.weizhi.tanhua.model.vo.PageResult;

import java.util.List;

/**
 * @Author milian
 * @Date 2021/10/27 0027 20:29
 */
@DubboService
public class CommentsApiImpl implements CommentsApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public PageResult findComments(String movementId, CommentType comment, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("publishId")
                .is(new ObjectId(movementId))
                .and("commentType")
                .is(comment.getType());
        Query query = Query.query(criteria)
                .skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));

        List<Comment> list = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageResult(page,pagesize,count,list);
    }
    @Override
    public Integer save(Comment commentObj) {
        Movement movement = mongoTemplate.findById(commentObj.getPublishId(), Movement.class);
        if (movement!=null){
            commentObj.setPublishUserId(movement.getUserId());
        }
        // 保存数据
        mongoTemplate.save(commentObj);
        // 更新动态表对应字段
        Query query = Query.query(Criteria.where("id").is(commentObj.getPublishId()));
        Update update = new Update();
        if(commentObj.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount",1);
        }else if (commentObj.getCommentType() == CommentType.COMMENT.getType()){
            update.inc("commentCount",1);
        }else {
            update.inc("loveCount",1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        //获取更新后的最新数据
        options.returnNew(true) ;
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        // 获取最新的评论数量，并返回
        return modify.statisCount(commentObj.getCommentType() );
    }

    @Override
    public Boolean hasComment(String movementId, Long id, int type) {
        Criteria criteria = Criteria.where("publishId")
                .is(new ObjectId(movementId))
                .and("userId")
                .is(id)
                .and("commentType")
                .is(type);
        Query query =Query.query(criteria);
        return mongoTemplate.exists(query, Comment.class);
    }

    @Override
    public Integer delete(String movementId, Long id, int type) {
        Criteria criteria = Criteria.where("publishId")
                .is(new ObjectId(movementId))
                .and("userId")
                .is(id)
                .and("commentType")
                .is(type);
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,  Comment.class);
        // 修改动态表数据
        Query movementQuery = Query.query(Criteria.where("id")
                .is(new ObjectId(movementId)));
        Update update = new Update();
        if (CommentType.LIKE.getType()==type){
            update.inc("likeCount", -1);
        }else if (CommentType.COMMENT.getType()==type){
            update.inc("commentCount", -1);
        }else {
            update.inc("loveCount",-1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true) ;//获取更新后的最新数据
        Movement modify = mongoTemplate.findAndModify(movementQuery, update, options, Movement.class);
        //5、获取最新的评论数量，并返回
        return modify.statisCount(type);
    }
}
